package io.renren.modules.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.car.entity.CarShopEntity;
import io.renren.modules.car.service.CarShopService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 商家基本信息
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-09-17 11:28:03
 */
@RestController
@RequestMapping("car/carshop")
public class CarShopController extends AbstractController {
    @Autowired
    private CarShopService carShopService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("car:carshop:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("create_user",getUserIdInt());
        PageUtils page = carShopService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("car:carshop:info")
    public R info(@PathVariable("id") Integer id){
        CarShopEntity carShop = carShopService.getById(id);

        return R.ok().put("carShop", carShop);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("car:carshop:save")
    public R save(@RequestBody CarShopEntity carShop){

        List<CarShopEntity> carShopEntityList = carShopService.list(new QueryWrapper<CarShopEntity>().eq("create_user", getUserIdInt()));

        if (carShopEntityList.size()>0){
            return R.error("目前一个账号只能创建一个商家");
        }

        Date date = new Date();
        carShop.setCreateTime(date);
        carShop.setUpdateTime(date);
        carShop.setCreateUser(getUserIdInt());
        carShopService.save(carShop);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("car:carshop:update")
    public R update(@RequestBody CarShopEntity carShop){
        ValidatorUtils.validateEntity(carShop);
        carShop.setUpdateTime(new Date());
        carShopService.updateById(carShop);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("car:carshop:delete")
    public R delete(@RequestBody Integer[] ids){
        carShopService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
